package com.chezhujie.www.controller;

import com.chezhujie.www.domain.SettleAccounts;
import com.chezhujie.www.service.CzjbusinessService;
import com.chezhujie.www.service.SettleAccountsService;
import com.sys.core.page.Pagination;
import com.sys.core.utils.ConvertObj;
import com.sys.core.utils.UUIDUtils;
import com.sys.user.base.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 *   结算
 * Created by 汤伟杰 on 2017/5/23.
 */
@Controller
@RequestMapping("/settleaccounts")
public class SettleAccountsController extends BaseController<SettleAccounts> {
    @Autowired
    private SettleAccountsService settleaccountsService;
    @Autowired
    private CzjbusinessService czjbusinessService;

    /** 列表 **/
//    @RequestMapping("/list.do")
    public String list(Integer pageNo, Integer pageSize, SettleAccounts settleaccounts, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(settleaccounts,req,model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(settleaccounts);
            Pagination pagination = settleaccountsService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取结算列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", settleaccounts);
        return "settleaccounts/list";
    }
    /**
    * api接口结算列表
    * @param pageNo
    * @param pageSize
    * @param settleaccounts
    * @param req
    * @param resp
    * @param model
    * @throws Exception
    */
//    @RequestMapping("/apiList.jn")
    public void apiList(Integer pageNo, Integer pageSize, SettleAccounts settleaccounts,
        HttpServletRequest req, HttpServletResponse resp,
        ModelMap model) throws Exception {
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(settleaccounts);
            Pagination pagination = settleaccountsService.apilist(pageNo.intValue(), pageSize.intValue(), params, model);
            jsonResult(1,"","",pagination,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"","获取结算列表失败!"+e,"",resp);
        }

    }
    /**
    * api接口结算详情
    * @param req
    * @param resp
    * @param model
    * @throws Exception
    */
//    @RequestMapping("/apiDetail.jn")
    public void apiDetail(HttpServletRequest req, HttpServletResponse resp,
    ModelMap model) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        Enumeration<String> enumeration = req.getParameterNames();
        if(enumeration.hasMoreElements()){
            String next = enumeration.nextElement();
            params.put(next, req.getParameter(next));
        }

        try {
            Map<String,Object> result = settleaccountsService.getApiDetail(params);
            jsonResult(1,"","",result,resp);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult(0,"99","获取结算详情失败!"+e,"",resp);
        }

    }
    /** api测试页面 **/
//    @RequestMapping("/apiTest.do")
    public String apiList(HttpServletRequest req, HttpServletResponse resp,
        ModelMap model) throws Exception {
        model.addAttribute("entity",new HashMap<String,Object>());
        return "settleaccounts/apiTest";

    }

    /** 保存 **/
//    @RequestMapping("/save.do")
    public String save(SettleAccounts settleaccounts,
                      HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        try {

            if(StringUtils.isNotBlank(settleaccounts.getSettleaccountsId())){
                settleaccountsService.update(settleaccounts);


            }else{
                settleaccounts.setSettleaccountsId(UUIDUtils.getUUID());
                settleaccountsService.insert(settleaccounts);
            }
            model.addAttribute("success", "保存结算成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "保存结算失败!详情："+e.getMessage());
        }
        return "redirect:list.do";
    }


    /** 详情 **/
//    @RequestMapping("/detail.do")
    public String detail(@RequestParam(required = true)String settleaccountsId,SettleAccounts settleaccounts, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(settleaccounts,req,model);
        try {
            SettleAccounts entity = settleaccountsService.queryById(settleaccounts.getSettleaccountsId());
            model.addAttribute("entity", entity);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询结算出错!详情："+e.getMessage());
        }
        return "settleaccounts/detail";
    }
    /** 新增或修改 **/
//    @RequestMapping("/add.do")
    public String add(SettleAccounts settleaccounts, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(settleaccounts,req,model);
        if(StringUtils.isNotBlank(settleaccounts.getSettleaccountsId())){
            settleaccounts = settleaccountsService.queryById(settleaccounts.getSettleaccountsId());
            model.addAttribute("entity", settleaccounts);
        }
        return "settleaccounts/add";
    }
    /** 删除 **/
//    @RequestMapping("/delete.do")
    public String delete(@RequestParam(required = true)String settleaccountsId,SettleAccounts settleaccounts, HttpServletRequest req, HttpServletResponse resp,
                     ModelMap model){
        try {
            settleaccountsService.deleteById(settleaccounts.getSettleaccountsId());
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "redirect:list.do";
    }

    private void setMsg(SettleAccounts settleaccounts,HttpServletRequest req, ModelMap model) {
        model.addAttribute("failed", req.getParameter("failed"));
        model.addAttribute("success", req.getParameter("success"));
        model.addAttribute("entity", settleaccounts);
    }

    /**
     * 后台结账信息列表
     * @param pageNo
     * @param pageSize
     * @param settleaccounts
     * @param req
     * @param resp
     * @param model
     * @return
     */
    @RequestMapping("/adminList.do")
    public String adminList(Integer pageNo, Integer pageSize, SettleAccounts settleaccounts, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){

        setMsg(settleaccounts,req,model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            //businessId
            Map<String, Object> params = ConvertObj.toMap(settleaccounts);
            params.put("delFlag",0);

            Pagination pagination = settleaccountsService.getByPage(pageNo.intValue(), pageSize.intValue(), params, model);
//            Pagination pagination = settleaccountsService.list1(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取结算列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", settleaccounts);
        return "settleaccounts/adminList";
    }

    /**
     * 前台结账信息列表
     * @param pageNo
     * @param pageSize
     * @param settleaccounts
     * @param req
     * @param resp
     * @param model
     * @return
     */
    @RequestMapping("/frontList.do")
    public String frontList(Integer pageNo, Integer pageSize, SettleAccounts settleaccounts, HttpServletRequest req, HttpServletResponse resp,
                        ModelMap model){

        setMsg(settleaccounts,req,model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(settleaccounts);
            Pagination pagination = settleaccountsService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取结算列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", settleaccounts);
        return "settleaccounts/list";
    }
}
